<?php
global $db;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
if (isset($_POST["user"])){
	session_start();
	if(!isset($_SESSION["vcode"]) || !isset($_SESSION["vcode"]) ||$_SESSION["vcode"] != $_POST["vcode"]) {
		echo "2";
		exit();
	}
	require_once "../tool/sql.php";
	
	if($db->connected()) {
        $res = $db->selectById("userinfo","passwd","name",$_POST["user"]);
		if(!empty($res)){foreach ($res as $real_wd) {
			echo "1";
			$db->close();
			exit();
		}}
		if(!$db->insert("userinfo",
            [   "name" => $_POST["user"],
                "passwd" => $_POST["passwd"],
                "learned_word_count" => 0,
                "public_cache" => ""
            ])) echo $db->getError();
        if(!$db->insert('wordlist',["owner_name"=>$_POST["user"],"list_id"=>0,"text"=>json_encode(array())]))
            die($db->getError());
        unlink("../img/vcode/".session_id().".png");
        echo "0";
	}else{
		echo $db->getConnectError();
		//echo "3";
		$db->close();
		exit();
	}
	$db->close();
	unset($db);
}

